<?php 
if (isset($_REQUEST['maFonction']) && $_REQUEST['maFonction'] != '')
{
    $_REQUEST['maFonction']($_REQUEST);
}

function createUser($data){	

	$role = $data['params']['role'];
	$password = $data['params']['passwd'];

	$apps = $data['params']['accessRights'];
	
	if (in_array("Go No Go", $apps))
		$rights = "1"; 
	else
		$rights = "0";
		
	if (in_array("Decision Making", $apps))
		$rights .= "1"; 
	else
		$rights .= "0";
	
	if (in_array("Wiki", $apps))
		$rights .= "1"; 
	else
		$rights .= "0";
	
	if (in_array("FOPViewer", $apps))
		$rights .= "1"; 
	else
		$rights .= "0";
		
	if (in_array("Log Monitoring", $apps))
		$rights .= "1"; 
	else
		$rights .= "0";

	if (in_array("TM Viewer", $apps))
		$rights .= "1"; 
	else
		$rights .= "0";
	
	$host = "localhost"; $user = "postgres"; 
	$pass = "admin"; $db = "postgres"; 
	$con = pg_connect("host=$host dbname=$db user=$user password=$pass");
	
	$query = "INSERT INTO authentication VALUES('$role', '$password','$rights');"; 
	$rs = pg_query($con, $query);
	pg_close($con);
	
	if ($rs == FALSE) 
		return TRUE;      
}

function alterRoleRights($data){	
	$role = $data['params']['role'];
	$apps=$data['params']['accessRights'];
	
	if (in_array("Go No Go", $apps))
		$rights = "1"; 
	else
		$rights = "0";
		
	if (in_array("Decision Making", $apps))
		$rights .= "1"; 
	else
		$rights .= "0";
	
	if (in_array("Wiki", $apps))
		$rights .= "1"; 
	else
		$rights .= "0";
	
	if (in_array("FOPViewer", $apps))
		$rights .= "1"; 
	else
		$rights .= "0";
		
	if (in_array("Log Monitoring", $apps))
		$rights .= "1"; 
	else
		$rights .= "0";

	if (in_array("TM Viewer", $apps))
		$rights .= "1"; 
	else
		$rights .= "0";
	
	$host = "localhost"; $user = "postgres"; 
	$pass = "admin"; $db = "postgres"; 
	$con = pg_connect("host=$host dbname=$db user=$user password=$pass"); 

	$query = "UPDATE authentication SET access_rights='$rights' WHERE role='$role';"; 
	$rs = pg_query($con, $query); 
	pg_close($con);
	
	if($rs == FALSE)
		return FALSE; // The role didn't exist.
	else 
		return TRUE; // Opertation successful.
}

function deleteRole($data){	
	// TODO: uncomment when integrating with the html page.
	//$role = $data['params']['role'];
	
	// TODO: delete when integrating with the html page.
	$role = 'nelson';
	
	$host = "localhost"; $user = "postgres"; 
	$pass = "admin"; $db = "postgres"; 
	$con = pg_connect("host=$host dbname=$db user=$user password=$pass"); 

	$query = "DELETE FROM authentication where role='$role';"; 
	$rs = pg_query($con, $query); 

	if($rs == FALSE)
		return FALSE; // The role didn't exist.
	else 
		return TRUE; // Operation successful.
}

function getRoleRights($data){	
	
	$role = $data['params']['role'];	
	$host = "localhost"; $user = "postgres"; 
	$pass = "admin"; $db = "postgres"; 
	$con = pg_connect("host=$host dbname=$db user=$user password=$pass"); 
	

	$query = "SELECT access_rights FROM authentication WHERE role='$role';"; 
	$rs = pg_query($con, $query); 
	pg_close($con);
	if($rs != FALSE){
		$row = pg_fetch_array($rs, 0);
		echo $row["access_rights"];
		return TRUE; // Operation successful.
	}
	else{ 
		return FALSE; // The role didn't exist.	
	}
}
?>
